#include "listitem.h"
#include "ui_listitem.h"
#include <QMenu>

ListItem::ListItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ListItem),isLike(false)
{
    ui->setupUi(this);
    this->setStyleSheet("background-color:rgb(196, 222, 255);");
    ui->moreBt->hide();
    //ui->moreBt->installEventFilter(this);

    menu = new QMenu(this);
    menu->setStyleSheet(
        "QMenu {"
        "   background-color: rgba(166, 197, 255,0.8);"        // 菜单背景色
        "   border: 1px solid #cccccc;"         // 菜单边框
        "}"
        "QMenu::item {"
        "   padding: 5px 20px;"                 // 菜单项内边距
        "   background-color: transparent;"    // 菜单项默认背景
        "}"
        "QMenu::item:selected {"
        "   background-color: #b0c4de;"         // 菜单项被选中时的背景色
        "}"
        "QMenu::separator {"
        "   height: 1px;"                       // 分隔线高度
        "   background: #cccccc;"               // 分隔线颜色
        "   margin: 5px 0;"                     // 分隔线上下外边距
        "}"
    );
    // 创建菜单项
    QAction *playAction = new QAction("播放", this);
    QAction *nextAction = new QAction("下一首播放", this);
    QAction *commentAction = new QAction("查看评论", this);
    QAction *removeFromPlaylistAction = new QAction("从歌单中删除", this);

    // 添加菜单项到菜单中
    menu->addAction(playAction);
    menu->addAction(nextAction);
    menu->addAction(commentAction);
    menu->addAction(removeFromPlaylistAction);

    connect(playAction,&QAction::triggered,this,[=](){
       emit itemPlayClick(this->musicId);
       qDebug()<<" emit itemPlayClick();";
    });
    connect(nextAction,&QAction::triggered,this,[=](){
       emit itemNextClick(this->musicId);
    });
    connect(removeFromPlaylistAction,&QAction::triggered,this,[=](){
       emit itemDeleteClick(this->musicId);
    });

    connect(commentAction,&QAction::triggered,this,[=](){
       emit itemCommendClick(this->musicId);
    });
    menu->hide();
}

ListItem::~ListItem()
{
    //qDebug()<<"~ListItem";
    delete ui;
}

void ListItem::enterEvent(QEvent *event)
{
    (void)event;
    this->setStyleSheet("background-color:rgb(133, 180, 255);");
    ui->moreBt->show();
}

void ListItem::leaveEvent(QEvent *event)
{
    (void)event;
    this->setStyleSheet("background-color:rgb(196, 222, 255);");
    ui->moreBt->hide();
}

//void ListItem::mousePressEvent(QMouseEvent *event)
//{
//    (void)event;
//    this->setStyleSheet("background-color:rgb(123, 194, 255);");
//}


void ListItem::setMusicNameLabel(const QString &musicName)
{
    ui->musicNameLabel->setText(musicName);
}

void ListItem::setSingerNameLabel(const QString &singerName)
{
    ui->singerNameLabel->setText(singerName);
}

void ListItem::setCoverNameLabel(const QString &coverName)
{
    ui->coverNameLabel->setText(coverName);
}

void ListItem::setIsLike(bool isLike)
{
    this->isLike=isLike;
    if(this->isLike)
    {
        ui->likeBt->setStyleSheet("#likeBt{border-image:url(\":/res/star1.png\");}");
    }
    else
    {
         ui->likeBt->setStyleSheet("#likeBt{border-image:url(\":/res/star2.png\");}");
    }
}

void ListItem::setMusicId(QString musicId)
{
    this->musicId=musicId;
}

QPoint ListItem::topRightPoint()
{
    QPoint topRight=ui->moreBt->geometry().topRight();

    return topRight;
}


void ListItem::on_likeBt_clicked()
{
    //qDebug()<<"void ListItem::on_likeBt_clicked()";
    isLike=!isLike;
    this->setIsLike(isLike);

    emit isLikeClick(this->isLike);
}

void ListItem::on_moreBt_clicked()
{


    //emit moreBtClick(menu,ui->moreBt->width(),ui->moreBt->height());
    //qDebug()<<"emit moreBtClick();";


    // 连接 "更多" 按钮的点击信号到菜单的弹出函数

    menu->exec(ui->moreBt->mapToGlobal(QPoint(ui->moreBt->width()-menu->width(), ui->moreBt->height())));  // 显示菜单

}

//void ListItem::onItemPlayClick()
//{

//}

//bool ListItem::eventFilter(QObject *watched, QEvent *event)
//{
//    if(watched==ui->moreBt)
//    {
//        if(event->type()==QEvent::MouseButtonPress)
//        {
//            QMenu *menu = new QMenu(this);
//            emit moreBtClick(menu,ui->moreBt->width(),ui->moreBt->height());
//            qDebug()<<"emit moreBtClick();";
//        }
//        return true;
//    }
//    return QObject::eventFilter(watched, event);
//}




















